﻿
namespace Common.Msg
{
    public class ResultInfo
    {
        private ResultType _resultType;
        private string _customMessageStack;
        private string _messageStack;
        /// <summary>
        /// 异常对象
        /// </summary>
        public System.Exception ExceptionInfo = new System.Exception();
        public ResultType ResultType
        {
            get
            {
                if (this._resultType == null)
                {
                    this._resultType = ResultType.Unspecified;
                }
                return this._resultType;
            }
            private set
            {
                this._resultType = value;
            }
        }
        public string CustomMessage
        {
            get;
            private set;
        }
        public string Message
        {
            get;
            private set;
        }
        public string CustomMessageStack
        {
            get
            {
                this.LoadResultStack(this, 1);
                return this._customMessageStack;
            }
        }
        public string MessageStack
        {
            get
            {
                this.LoadResultStack(this, 1);
                return this._messageStack;
            }
        }
        public ResultInfo InnerResult
        {
            get;
            private set;
        }
        private void LoadResultStack(ResultInfo reult, int index)
        {
            object customMessageStack = this._customMessageStack;
            this._customMessageStack = string.Concat(new object[]
            {
                customMessageStack,
                index,
                ".",
                reult.CustomMessage,
                "\\n；"
            });
            object messageStack = this._messageStack;
            this._messageStack = string.Concat(new object[]
            {
                messageStack,
                index,
                ".",
                reult.Message,
                "\\n；"
            });
        }
        /// <summary>
        /// 错误详情上报
        /// </summary>
        /// <param name="message">错误信息</param>
        public void Alert(string msg)
        {
            this.CustomMessage = msg;
            this.Message = msg + "  \\n" + this.ResultType.Info;
        }
        public void Alert(string msg, ResultInfo result)
        {
            this.InnerResult = result;
            this.Alert(msg);
        }
        public void Alert(ResultInfo result)
        {
            this.InnerResult = result;
        }
        /// <summary>
        /// 错误详情上报
        /// </summary>
        /// <param name="restulType">错误类型</param>
        /// <param name="message">错误信息</param>
        public void Alert(ResultType restulType, string msg, System.Exception ex = null)
        {
            this.CustomMessage = msg;
            this.ResultType = restulType;
            this.ExceptionInfo = ex;
            this.Message = msg + "  \\n" + this.ResultType.Info;
        }
        public void Alert(ResultType restulType, string msg, ResultInfo result)
        {
            this.InnerResult = result;
            this.Alert(restulType, msg, (System.Exception)null);
        }
        /// <summary>
        /// 错误详情上报
        /// </summary>
        /// <param name="restulType">错误类型</param>
        public void Alert(ResultType restulType)
        {
            this.ResultType = restulType;
            this.Message = this.ResultType.Info;
        }
        public void Alert(ResultType restulType, ResultInfo result)
        {
            this.InnerResult = result;
            this.Alert(restulType);
        }
    }
    [System.Diagnostics.DebuggerDisplay("ResultType={Info}")]
    public class ResultType
    {
        public int ResultCode { get; private set; }
        public string CustomInfo { get; private set; }
        public string Info
        {
            get
            {
                return string.Concat(new object[]
                {
                    "{错误代码：",
                    this.ResultCode,
                    " -- ",
                    this.CustomInfo,
                    "}"
                });
            }
        }
        /// <summary>
        /// 操作成功[1]
        /// </summary>
        public static ResultType Success { get { return new ResultType(1, "操作成功"); } }
        /// <summary>
        /// 未指定[0000]
        /// </summary>
        public static ResultType Unspecified { get { return new ResultType(0, "未指定"); } }
        /// <summary>
        /// 系统异常[0001]
        /// </summary>
        public static ResultType SystemException { get { return new ResultType(1, "系统异常"); } }
        /// <summary>
        /// 数据库录入失败[1001]
        /// </summary>
        public static ResultType DataBaseInsertFail { get { return new ResultType(1001, "数据库录入失败"); } }
        /// <summary>
        /// 数据库更新失败[1002]
        /// </summary>
        public static ResultType DataBaseUpdateFail { get { return new ResultType(1002, "数据库更新失败"); } }
        /// <summary>
        /// 数据库删除失败[1003]
        /// </summary>
        public static ResultType DataBaseDeleteFail { get { return new ResultType(1003, "数据库删除失败"); } }
        /// <summary>
        /// 数据库查询失败[1004]
        /// </summary>
        public static ResultType DataBaseSelectFail { get { return new ResultType(1004, "数据库查询失败"); } }
        /// <summary>
        /// 数据库查不到数据[1005]
        /// </summary>
        public static ResultType DataBaseNotFindData { get { return new ResultType(1005, "数据库查不到数据"); } }
        /// <summary>
        /// 数据库查不到数据[1006]
        /// </summary>
        public static ResultType DataBaseOperationFail { get { return new ResultType(1006, "数据库操作失败"); } }
        public ResultType()
        {
            ResultType type = ResultType.Unspecified;
            this.ResultCode = type.ResultCode;
            this.CustomInfo = type.CustomInfo;
        }
        public ResultType(int code, string info)
        {
            this.ResultCode = code;
            this.CustomInfo = info;
        }
        public override string ToString()
        {
            return this.Info;
        }
    }
}
